/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.ad.domain.SysDomain;
import cn.ibizlab.core.ad.service.SysDomainService;
import cn.ibizlab.core.ad.filter.SysDomainSearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[SysDomain] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysDomainResource {

    @Autowired
    public SysDomainService sysDomainService;

    @Autowired
    @Lazy
    public SysDomainDTOMapping sysDomainDtoMapping;

    @Autowired
    @Lazy
    public SysDomainFilterDTOMapping sysDomainFilterDtoMapping;


    /**
    * 创建Create 域
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysDomainDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"域" },  notes = "SysDomain-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDomain-Create-all') or hasPermission(this.sysDomainDtoMapping.toDomain(#dto),'uaa-SysDomain-Create')")
    @PostMapping(value = {"sys_domains","sys_domains"})
    public ResponseEntity<SysDomainDTO> create
            (@Validated @RequestBody SysDomainDTO dto) {
        SysDomain domain = sysDomainDtoMapping.toDomain(dto);
        sysDomainService.create(domain);
        SysDomain rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysDomainDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 域
    * 
    *
    * @param id id
    * @return ResponseEntity<SysDomainDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"域" },  notes = "SysDomain-Get ")
    @PostAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDomain-Get-all') or hasPermission(this.sysDomainDtoMapping.toDomain(returnObject.body),'uaa-SysDomain-Get')")
    @GetMapping("sys_domains/{id}")
    public ResponseEntity<SysDomainDTO> getById
            (@PathVariable("id") String id) {
        SysDomain rt = sysDomainService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysDomainDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 域
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"域" },  notes = "SysDomain-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDomain-Remove-all') or hasPermission(this.sysDomainService.get(#id),'uaa-SysDomain-Remove')")
    @DeleteMapping("sys_domains/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysDomainService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 域
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysDomainDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"域" },  notes = "SysDomain-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDomain-Update-all') or hasPermission(this.sysDomainService.get(#id),'uaa-SysDomain-Update')")
    @VersionCheck(entity = "sysdomain" , versionfield = "updateTime")
    @PutMapping("sys_domains/{id}")
    public ResponseEntity<SysDomainDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysDomainDTO dto) {
        SysDomain domain = sysDomainDtoMapping.toDomain(dto);
        domain.setId(id);
        sysDomainService.update(domain);
        SysDomain rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysDomainDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 域
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"域" },  notes = "SysDomain-CheckKey ")
    @PostMapping("sys_domains/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysDomainDTO dto) {
        SysDomain domain = sysDomainDtoMapping.toDomain(dto);
        Integer rt = sysDomainService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 域
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysDomainDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"域" },  notes = "SysDomain-GetDraft ")
    @GetMapping("sys_domains/getdraft")
    public ResponseEntity<SysDomainDTO> getDraft
            (@SpringQueryMap SysDomainDTO dto) {
        SysDomain domain = sysDomainDtoMapping.toDomain(dto);
        SysDomain rt = sysDomainService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysDomainDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 域
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysDomainDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"域" },  notes = "SysDomain-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDomain-Save-all') or hasPermission(this.sysDomainDtoMapping.toDomain(#dto),'uaa-SysDomain-Save')")
    @PostMapping("sys_domains/save")
    public ResponseEntity<SysDomainDTO> save
            (@Validated @RequestBody SysDomainDTO dto) {
        SysDomain domain = sysDomainDtoMapping.toDomain(dto);
        sysDomainService.save(domain);
        SysDomain rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysDomainDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault 域
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysDomainDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"域" },  notes = "SysDomain-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDomain-FetchDefault-all') or hasPermission(#dto,'uaa-SysDomain-FetchDefault')")
    @PostMapping("sys_domains/fetchdefault")
    public ResponseEntity<List<SysDomainDTO>> fetchDefault
            (@Validated @RequestBody SysDomainFilterDTO dto) {
        SysDomainSearchContext context = sysDomainFilterDtoMapping.toDomain(dto);
        Page<SysDomain> domains = sysDomainService.searchDefault(context) ;
        List<SysDomainDTO> list = sysDomainDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建域
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDomain-Create-all')")
    @ApiOperation(value = "批量新建域", tags = {"域" },  notes = "批量新建域")
	@PostMapping("sys_domains/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysDomainDTO> dtos) {
        sysDomainService.createBatch(sysDomainDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除域
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDomain-Remove-all')")
    @ApiOperation(value = "批量删除域", tags = {"域" },  notes = "批量删除域")
	@DeleteMapping("sys_domains/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysDomainService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新域
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDomain-Update-all')")
    @ApiOperation(value = "批量更新域", tags = {"域" },  notes = "批量更新域")
	@PutMapping("sys_domains/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysDomainDTO> dtos) {
        sysDomainService.updateBatch(sysDomainDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存域
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDomain-Save-all')")
    @ApiOperation(value = "批量保存域", tags = {"域" },  notes = "批量保存域")
	@PostMapping("sys_domains/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysDomainDTO> dtos) {
        sysDomainService.saveBatch(sysDomainDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入域
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDomain-Save-all')")
    @ApiOperation(value = "批量导入域", tags = {"域" },  notes = "批量导入域")
	@PostMapping("sys_domains/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysDomainDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysDomainService.importData(config,ignoreError,sysDomainDtoMapping.toDomain(dtos)));
    }

}
